<?php

namespace App\Http\Controllers;

use App\Http\Requests\LinkStatisticRequest;
use App\Http\Requests\LiveRequest;
use App\Models\Activity;
use App\Models\Link;
use App\Models\LiveUser;
use App\Models\Beacon;
use App\Models\User;
use Illuminate\Http\Request;
use \DB;
use App\Http\Controllers\Controller;
use App\Models\Live;
use Illuminate\Pagination\LengthAwarePaginator as lPaginator;

class LinkStatisticController extends CommonController
{
    /**
     * 获取点击页面
     */
    public function getIndex(Request $request)
    {
        $links = Link::get();

        $datas = array();
        foreach ($links as $k => $v) {
            $count = Beacon::where('logUrl', $v->link)->count();
            $v->count = $count;
            $datas[] = $v;
        }

        $page = $request->input('page') ? $request->input('page') : 1;
        $paginator = new lPaginator(
            array_slice($datas, ($page - 1) * 10, 10),
            count($datas),
            10,
            $page,
            [
                'path' => 'linkstatistic',
//            'query' => array('uid' => $data['uid'], 'chatroom_id' => $data['chatroom_id']),
            ]
        );

        return view('linkstatistic.index')->with('lists', $paginator)->with('actions', $this->actions);
    }


    public function getCreate()
    {

        return view('linkstatistic.create');
    }


    public function postCreate(LinkStatisticRequest $request)
    {
        $data = $request->input();

        Link::create(['link'=>$data['link'], 'remark'=>$data['remark']]);

        return AjaxCallbackMessage('创建成功', true, url('linkstatistic/index'));
    }


    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = Link::where('id', $data['id'])->first();

        return view('linkstatistic.edit')->with('list', $list);
    }


    public function postEdit(LinkStatisticRequest $request)
    {
        $data = $request->input();

        Link::where('id', $data['id'])->update(['link'=>$data['link'], 'remark'=>$data['remark']]);

        return AjaxCallbackMessage('编辑成功', true, url('linkstatistic/index'));
    }

}
